<template>
  <div>
    <el-dialog :destroy-on-close="true" :lock-scroll="false" title="审核" :visible.sync="visible" width="900px"
               :before-close="cancel">
      <div style="height: 36px">申请信息：</div>
      <el-row :gutter="30" style="margin-left: 5px">
        <el-col :span="8">
          申请日期：{{ data.apply_date }}
        </el-col>
        <el-col :span="8">
          申请编号：{{ data.number }}
        </el-col>
        <el-col :span="8">
          申请类型：{{ data.type_text }}
        </el-col>
      </el-row>
      <el-row :gutter="30" style="margin-left: 5px">
        <el-col :span="8">
          申 请  人：{{ data.create_user_name }}
        </el-col>
        <el-col :span="8">
          目标仓库：{{ data.target_warehouse_name }}
        </el-col>
        <el-col :span="8">
          办理状态：{{ data.status_text }}
        </el-col>
      </el-row>
      <el-row :gutter="30" style="margin-left: 5px" >
        <el-col :span="8">
          申请说明：{{ data.apply_date }}
        </el-col>
      </el-row>
      <div>
        <span style="min-height: 36px;display: block;margin-top: 7px"> 备件清单：</span>
          <el-table
            :header-cell-style="{'text-align':'center'}"
            :cell-style="{'textAlign':'center'}"
            :data="data.items"
            style="width: 100%"
          >
            <el-table-column
              width="220"
              label="办理数量">
              <template v-slot="scope">
                  <el-input-number v-model="scope.row.dealQuantity" :min="0" :max="scope.row.active_quantity"></el-input-number>
              </template>
            </el-table-column>

            <el-table-column
              label="备件信息"
              width="220">
              <template #default="scope">
                {{ scope.row.name }} / {{ scope.row.c_number }} / {{ scope.row.model }}
              </template>
            </el-table-column>

            <el-table-column
              prop="quantity"
              label="申请数量">
            </el-table-column>

            <el-table-column
              prop="action_quantity"
              width="180"
              label="已办理数量">
            </el-table-column>

            <el-table-column
              width="180"
              prop="warehouse_name"
              label="原仓库">
            </el-table-column>

            <el-table-column
              width="180"
              prop="inventory"
              label="原仓库存">
            </el-table-column>

          </el-table>
      </div>

      <el-form label-width="auto" style="margin-top: 20px">
        <el-form-item label="办理说明:" prop="reason">
          <el-input placeholder="请输入办理说明" type="textarea" v-model="form.reason" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="danger" @click="reject">拒绝</el-button>
        <el-button :loading="loading" type="primary" @click="deal">办理</el-button>
      </div>
    </el-dialog>

    <reject ref="reject" type="sce" @done="sceCancel"></reject>
  </div>
</template>

<script>
import Reject from "@/views/bom/receipt/components/reject.vue";

export default {
  components: {Reject},
  props: {},
  data() {
    return {
      id: '',
      number: 1,
      data: {
        items: [{
          dealQuantity: null,
        }]
      },
      visible: false,
      loading: false,
      form: {
        reason:''
      },
    }
  },
  methods: {
    deal() {
      this.loading = true
      let items = (this.data.items.filter(item=>item.dealQuantity!==0)).map(item => {
          return {id: item.id, quantity: item.dealQuantity}

      })
      this.$http.post(`/inventories_audit/${this.id}/accept`, {
        description: this.form.description,
        items: items
      }).then((res) => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg)
          this.$emit('reload')
        } else {
          this.$message.error(res.data.msg)
        }
        this.cancel()
      })
      /*发起请求*/
      this.cancel()
    },
    active(info) {
      this.visible = true;
      //请求接口
      this.data = JSON.parse(JSON.stringify({...info}))
      this.id = info.id
      this.data.items.forEach((item) => {
        this.$set(item,'dealQuantity',0)
      })
    },
    reject() {
      this.$refs.reject.active(this.data.id)
    },
    cancel() {
      Object.assign(this.$data, this.$options.data())
      this.visible = false
    },
    sceCancel(){
      this.cancel()
      this.$emit('reload')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  .el-col {
    font-size: 13px;
    min-height: 36px;
  }
}

.el-select {
  width: 100%;
}


</style>
